package com.meisupic.copyright.util;

import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import org.assertj.core.util.Lists;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * creation wuxun
 * @version V1.0
 * @Description: 对象转换工具
 * @date 2018/1/14 15:41
 */
@Component
public class BeanMapper {



    private static Mapper dozer;

    @Autowired
    private Mapper mapper;

    @PostConstruct
    public void init() {
        BeanMapper.dozer = this.mapper;

    }
    /**
     * 基于Dozer转换对象的类型.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return dozer.map(source, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    @SuppressWarnings("rawtypes")
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        List<T> destinationList = Lists.newArrayList();
        for (Object sourceObject : sourceList) {
            T destinationObject = dozer.map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }

    /**
     * 基于Dozer将对象A的值拷贝到对象B中.
     */
    public static void copy(Object source, Object destinationObject) {
        dozer.map(source, destinationObject);
    }
}
